package com.itetchologyandenglish.home.service.Impl;

import com.itetchologyandenglish.home.enetity.Orders;
import com.itetchologyandenglish.home.enetity.TotalOrder;
import com.itetchologyandenglish.home.mapper.TotalMapper;
import com.itetchologyandenglish.home.service.TotalService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class TotalServiceImpl implements TotalService {
    @Resource
    TotalMapper totalMapper;
    @Override
    public Map<String, Integer> totalOrderClick() {
        //实例:订单_企业   123
        //1 采购   2.供应
        Integer count1=totalMapper.getOrderClick(1);
        if(count1==null){
            count1=0;
        }
        Integer count2=totalMapper.getOrderClick(2);
        if(count2==null){
            count2=0;
        }
        Integer sum=count1+count2;
        Map<String,Integer> map=new HashMap<>();
        map.put("采购订单访问量-注册企业",count1);
        map.put("供应订单访问量-注册企业",count2);
        map.put("订单总访问量-采购+供应",sum);
        return map;
    }
     /*
     * 组装一年的数据
     * */
    @Override
    public List<TotalOrder> totalOrderByYear(Integer year, Integer supplyFlag, Integer tradeFlag) {
        List<TotalOrder> listSearch=totalMapper.totalOrderByYear(year,supplyFlag,tradeFlag);
        List<TotalOrder> listReturn=new ArrayList<>(12);
        for(int i=1;i<=12;i++){
            TotalOrder totalOrder=new TotalOrder();
            totalOrder.setMonth(i);
            //遍历我所需要的月份
            for(int j=0;j<listSearch.size();j++){
                TotalOrder t=listSearch.get(j);
                //如果找寻到说明数据库当月不是空，给to赋值
                if(t.getMonth()==i){
                    totalOrder.setValue(t.getValue());
                    totalOrder.setYear(t.getYear());
                    break;
                }
            }
            listReturn.add(totalOrder);//添加每个月份的值
        }
        return listReturn;
    }

    @Override
    public List<String> totalOrderYears() {
        return totalMapper.totalOrderYears();
    }

    @Override
    public List<String> totalCompanyYears() {
        return  totalMapper.totalCompanyYears();
    }

    @Override
    public Map<String, Integer> totalOrder() {
        //1 采购总量   2.供应总量 3.已经完成 1  4.未完成  0 sum 总量
        Integer count1=totalMapper.getOrder(1);
        if(count1==null){
            count1=0;
        }
        Integer count2=totalMapper.getOrder(2);
        if(count2==null){
            count2=0;
        }
        Integer count3=totalMapper.getOrderByTrade(1);
        if(count3==null){
            count3=0;
        }
        Integer count4=totalMapper.getOrderByTrade(0);
        if(count4==null){
            count4=0;
        }
        Integer sum=count1+count2;
        Map<String,Integer> map=new HashMap<>();
        map.put("订单总量-采购+供应 已完成+未完成",sum);
        map.put("已完成订单总量-采购+供应",count3);
        map.put("未完成订单总量-采购+供应",count4);
        map.put("采购订单总量-已完成+未完成",count1);
        map.put("供应订单总量-已完成+未完成",count2);
        return map;
    }

    @Override
    public Map<String, Integer> totalCompany() {
        //1.vip 2.非vip sum.总量
        Integer count1=totalMapper.getCompany(1);
        if(count1==null){
            count1=0;
        }
        Integer count2=totalMapper.getCompany(0);
        if(count2==null){
            count2=0;
        }
        Integer sum=count1+count2;
        //暂时先这样处理，后续还有饼图没有做
        Map<String,Integer> map=new HashMap<>();
        map.put("用户总数量-注册企业 ",sum);
        map.put("VIP用户总量-注册企业",count1);
        map.put("普通用户总量-注册企业",count2);
        return map;
    }

    @Override
    public List<TotalOrder> totalCompanyByDate(Integer year, Integer isvip) {
        List<TotalOrder> listSearch=totalMapper.totalCompanyByDate(year,isvip);
        List<TotalOrder> listReturn=new ArrayList<>(12);
        for(int i=1;i<=12;i++){
            TotalOrder totalOrder=new TotalOrder();
            totalOrder.setMonth(i);
            //遍历我所需要的月份
            for(int j=0;j<listSearch.size();j++){
                TotalOrder t=listSearch.get(j);
                //如果找寻到说明数据库当月不是空，给to赋值
                if(t.getMonth()==i){
                    totalOrder.setValue(t.getValue());
                    totalOrder.setYear(t.getYear());
                    break;
                }
            }
            listReturn.add(totalOrder);//添加每个月份的值
        }
        return listReturn;
    }

    @Override
    public List<Orders> getTopClick(Integer n,Integer supplyFlag) {
        return totalMapper.getTopClick(n,supplyFlag);
    }
}
